<template>
  <el-dialog width="60%" top="10vh" :title="!dataForm.id ? '新增' : '修改'" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="100px">
      <el-form-item label="身份证号码" prop="idCard">
        <el-input v-model="dataForm.idCard" placeholder="请输入身份证号码" readonly></el-input>
      </el-form-item>
      <el-form-item label="城市名称" prop="cityName">
        <el-input v-model="dataForm.cityName" placeholder="请输入城市名称" readonly></el-input>
      </el-form-item>
      <el-form-item label="身份证正面" prop="frontPic">
        <el-image style="width: 300px; height: 150px;" :src="baseApi + dataForm.frontPic"
          :preview-src-list="[baseApi + dataForm.frontPic]" fit="contain" lazy>
          <div slot="error">
            <i class="el-icon-document" />
          </div>
        </el-image>
      </el-form-item>
      <el-form-item label="身份证反面" prop="backPic">
        <el-image style="width: 300px; height: 150px;" :src="baseApi + dataForm.backPic"
          :preview-src-list="[baseApi + dataForm.backPic]" fit="contain" lazy>
          <div slot="error">
            <i class="el-icon-document" />
          </div>
        </el-image>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="dataFormSubmit()">确定</el-button>
      <el-button size="small" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getAuthInfo, createAuth, updateAuth } from '@/api/knowledge/auth';
const baseUrl = process.env.VUE_APP_FILE_URL === '/' ? '' : process.env.VUE_APP_FILE_URL;

const defaultDataForm = {
  id: '',
  userId: '',
  idCard: '',
  cityId: '',
  cityName: '',
  frontPic: '',
  backPic: '',
}

export default {
  data() {
    return {
      dataForm: Object.assign({}, defaultDataForm),
      visible: false,
      baseApi: baseUrl,
      dataRule: {
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: '身份证号码不能为空', trigger: 'blur' }
        ],
        cityId: [
          { required: true, message: '城市ID不能为空', trigger: 'blur' }
        ],
        cityName: [
          { required: true, message: '城市名称不能为空', trigger: 'blur' }
        ],
        frontPic: [
          { required: true, message: '身份证正面不能为空', trigger: 'blur' }
        ],
        backPic: [
          { required: true, message: '身份证反面不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getAuthInfo(this.dataForm.id).then(res => {
            if (res && res.code === 200) {
              this.dataForm.userId = res.data.userId;
              this.dataForm.idCard = res.data.idCard;
              this.dataForm.cityId = res.data.cityId;
              this.dataForm.cityName = res.data.cityName;
              this.dataForm.frontPic = res.data.frontPic;
              this.dataForm.backPic = res.data.backPic;
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let params = {
            id: this.dataForm.id,
            userId: this.dataForm.userId,
            idCard: this.dataForm.idCard,
            cityId: this.dataForm.cityId,
            cityName: this.dataForm.cityName,
            frontPic: this.dataForm.frontPic,
            backPic: this.dataForm.backPic,
          }
          if (!this.dataForm.id) {
            // 创建
            createAuth(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.dataForm = Object.assign({}, defaultDataForm);
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          } else {
            // 更新
            updateAuth(params).then(response => {
              this.$refs['dataForm'].resetFields();
              this.$message({
                message: '修改成功',
                type: 'success',
                duration: 1000
              })
              this.visible = false
              this.$emit('refreshDataList')
            })
          }
        }
      })
    },
  }
}
</script>
